package com.easychat;

import com.easychat.utils.RedisUtil;
import com.easychat.websocket.netty.NettyWebSocketStarter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @Author: yudaxian
 * @Description: TODO
 * @DateTime: 2024/8/8 20:48
 **/
@Slf4j
@Component("initRunApplication")
public class InitRunApplication implements ApplicationRunner {

    @Resource
    private DataSource dataSource;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private NettyWebSocketStarter nettyWebSocketStarter;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        try {
            dataSource.getConnection();
            redisUtil.get("test");
            new Thread(nettyWebSocketStarter).start();
            log.info("--->初始化完成");
        } catch (SQLException e) {
            log.error("数据库连接失败:", e);
        } catch (RedisConnectionFailureException e) {
            log.error("redis连接失败:", e);
        } catch (Exception e) {
            log.error("初始化失败:", e);
        }
    }
}
